package Test2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

import com.sun.glass.ui.Menu;

import day2test1.Student;

//1-进书、办理借书卡
//2-借书和还书功能
//3-查询某书的借阅记录
//4-查询谋卡的借阅记录
//5-按借阅次数，显示图书信息
public class Main {
	static Scanner scanner = new Scanner(System.in);
	static ArrayList<Book> books = new ArrayList<Book>();
	static ArrayList<Card> cards = new ArrayList<Card>();
	static JDBCUile jdbcUile = new JDBCUile();

	// 进书功能
	public static void inputBook() {
		while (true) {
			System.out.println("请输入书的编号");
			int id = Integer.parseInt(scanner.nextLine());
			System.out.println("请输入书名");
			String name = scanner.nextLine();
			System.out.println("请输入库存");
			int repetory = Integer.parseInt(scanner.nextLine());

			Book book = new Book();

			book.setId(id);
			book.setName(name);
			book.setRepertory(repetory);
			jdbcUile.insertSingleBook(book);

			System.out.println("是否继续添加? (yes/no)");
			String flag = scanner.nextLine();
			if ("no".equals(flag)) {
				break;
			}
		}

	}

//办理借书卡
	public static void setCard() throws Exception {

		while (true) {
			Card card = new Card();
			System.out.println("请输入卡号");
			int id = Integer.parseInt(scanner.nextLine());
			System.out.println("请输入持卡人姓名");
			String name = scanner.nextLine();
			System.out.println("请输入持卡人班级");
			String classroom = scanner.nextLine();
			System.out.println("请输入持卡人专业");
			String major = scanner.nextLine();
			card.setId(id);
			card.setName(name);
			card.setClassRoom(classroom);
			card.setMajor(major);
			jdbcUile.insertSingleCard(card);

			System.out.println("办理成功,是否继续办理?(yes/no)");
			String flag = scanner.nextLine();
			if ("no".equals(flag))
				break;
		}
	}

//借书功能
	public static void borrowBook() throws Exception {

		System.out.println("请输入借书卡卡号");
		int id = Integer.parseInt(scanner.nextLine());
		System.out.println("请输入借书人名");
		String name = scanner.nextLine();
		Card card = new Card();
		card.setId(id);
		card.setName(name);
		card = jdbcUile.selectSingleCard(card);

		int flag1 = 0;

		// 先查询借书卡是否舒服正确
		if (card.getId() != null) {
			flag1 = 1;
			System.out.println("请输入要借的图书姓名");
			String bookname = scanner.nextLine();
			System.out.println("请输入要借的图书的id");
			int bookId = Integer.parseInt(scanner.nextLine());
			int flag2 = 0;
			Book book = new Book();
			book.setName(bookname);
			book.setId(bookId);

			book = jdbcUile.selectSingleBook(book);

			int repertory = book.getRepertory();

			if (repertory > 0) {
				System.out.println("请输入借书时间");
				String timeStart = scanner.nextLine();

				CardRecord cardRecord = new CardRecord();

				cardRecord.setCardId(id);
				cardRecord.setName(name);
				cardRecord.setTimeStart(timeStart);
				cardRecord.setTimeEnd(null);
				cardRecord.setBookId(bookId);
				cardRecord.setBookName(bookname);

				jdbcUile.insertSingleCardRecord(cardRecord);

				book.setRepertory(repertory - 1);

				jdbcUile.updateBook(book);

				System.out.println("借阅成功!");
			} else {
				System.out.println("图书库存不够，借阅失败!");
			}

		}

		if (flag1 == 0) {
			System.out.println("借阅失败，借书卡卡号不存在");
		}
	}
	// 还书功能

	public static void returnBook() throws Exception {
		System.out.println("请输入借书卡卡号");
		int id = Integer.parseInt(scanner.nextLine());
		System.out.println("请输入您的名字");
		String name = scanner.nextLine();

		Card card = new Card();
		card.setId(id);
		card.setName(name);
		card = jdbcUile.selectSingleCard(card);

		// 先查询借书卡是否正确
		if (card.getId() != null) {
			System.out.println("请输入要还的图书姓名");
			String bookname = scanner.nextLine();
			System.out.println("请输入要还的图书的编号");
			int bookid = Integer.parseInt(scanner.nextLine());
			Book book = new Book();
			book.setId(bookid);
			book.setName(bookname); 
			book = jdbcUile.selectSingleBook(book);

			// 检查该书时候在图书馆中存在
			if (book.getName() != null) {

				System.out.println("请输入还书时间");
				String timeEnd = scanner.nextLine();

				CardRecord cardRecord = new CardRecord();
				cardRecord.setBookId(bookid);
				cardRecord.setBookName(bookname);
				cardRecord.setCardId(id);
				cardRecord.setName(name);
				cardRecord.setTimeEnd(timeEnd); // 更新还书时间

			   int flag =	jdbcUile.updateCardRecord(cardRecord);
			   if(flag ==1) {
				int repertory = book.getRepertory();// 获取 图书的当前库存
				book.setRepertory(repertory + 1);
				JDBCUile.updateBook(book); // 更新库存 
			   }else {
				System.out.println("还书失败");
			}

				
				
				

			} else {
				System.out.println("图书不存在");

			}

		} else {
			System.out.println("借书卡输入不正确");
		}

	}

	// 查询某书的借阅记录
	public static void findBook() {
		System.out.println("请输入要查询的图书的名称:");
		String bookname = scanner.nextLine();
		int flag = 0;
		System.out.println("请输入要查询的书的编号");
		int id = Integer.parseInt(scanner.nextLine());
		ArrayList<CardRecord> bookRecords = new ArrayList<>();
		bookRecords = jdbcUile.selectBookRecord(bookname, id);
		for (CardRecord bookRecord : bookRecords) {
			int cardId = bookRecord.getCardId();
			String cardname = bookRecord.getName();
			int bookId = bookRecord.getBookId();
			String timeStart = bookRecord.getTimeStart();
			String timeEnd = bookRecord.getTimeEnd();
			if (cardname != null) {
				
				if(timeEnd!=null) {
				System.out.println("书名: " + bookname + " 书编号 ：" + bookId + " 借书人： " + cardname + " 借书人编号: " + cardId
						+ " 借书时间: " + timeStart + " 还书时间： " + timeEnd);
				}else {
					System.out.println("书名: " + bookname + " 书编号 ：" + bookId + " 借书人： " + cardname + " 借书人编号: " + cardId
							+ " 借书时间: " + timeStart + " 未还书 " );
				}
			} else {
				System.out.println("查询失败");
				break;
			}

		}

	}

	// 查询谋卡的借阅记录
	public static void findCard() throws Exception {

		System.out.println("请输入借书人的名字");
		String name = scanner.nextLine();
		System.out.println("请输入借书卡的卡号");
		int id = Integer.parseInt(scanner.nextLine());
		ArrayList<CardRecord> cardRecords = jdbcUile.selectAllCardRecords(name, id);
		System.out.println("姓名：" + name + " 卡号：" + id + " 借阅的书籍有： ");
		for (CardRecord cardRecord : cardRecords) {
			int cardId = cardRecord.getCardId();
			String cardname = cardRecord.getName();
			int bookId = cardRecord.getBookId();
			String timeStart = cardRecord.getTimeStart();
			String timeEnd = cardRecord.getTimeEnd();
			String bookName = cardRecord.getBookName();
			if (cardname != null) {
				if (timeEnd != null) {
					
					System.out.println("正在借阅 ：  书名 ："+bookName+" 编号: "+bookId+" 借阅时间: "+timeStart);
					

				}else {
					System.out.println("已经完成借阅 ： 书名 ："+bookName+" 编号: "+bookId+" 借阅时间: "+timeStart+" 还书时间 ： "+timeEnd);
					
				}

			} else {
				System.out.println("查询失败");
				break;
			}

		}

	}

	// 按借阅次数，显示图书信息
	public static void showAll() throws Exception {
		ArrayList<StringBuffer> lists = new ArrayList<>();
		
		lists = jdbcUile.selectBookBorrowSum();
		
		for(StringBuffer sb :lists) {
			System.out.println(sb.toString());
		}
		
	}

	// 主界面
	public static void Meum() throws Exception {
		// 1-进书、办理借书卡
		// 2-借书和还书功能
		// 3-查询某书的借阅记录
		// 4-查询谋卡的借阅记录
		// 5-按借阅次数，显示图书信息
		while (true) {
			System.out.println("------------\t欢迎使用图书馆管理系统\t----------------");
			System.out.println("------------\t请输入命令进行功能选择\t----------------");
			System.out.println("------------\t 1.进书\t\t\t----------------");
			System.out.println("------------\t 2.办理借书卡\t\t----------------");
			System.out.println("------------\t 3.借书\t\t\t----------------");
			System.out.println("------------\t 4.还书\t\t\t----------------");
			System.out.println("------------\t 5.查询借阅记录（书籍）\t----------------");
			System.out.println("------------\t 6.查询借阅记录（借书卡）\t----------------");
			System.out.println("------------\t 7.按照借阅次数显示所有图书信息----------------");
			System.out.println("------------\t #.退出\t\t\t----------------");

			String choose = scanner.nextLine();
			switch (choose) {

			case "1": {
				inputBook();
				break;
			}
			case "2": {
				setCard();
				break;
			}
			case "3": {
				borrowBook();
				break;
			}
			case "4": {
				returnBook();
				break;
			}
			case "5": {
				findBook();
				break;
			}
			case "6": {
				findCard();
				break;
			}
			case "7": {
				showAll();
				break;
			}
			case "#": {
				return;
			}
			default:
				throw new IllegalArgumentException("输入错误: " + choose);
			}
		}
	}

	public static void main(String[] args) throws Exception {
		Meum();
	}
}
